﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using DTO;
using System.Data.SqlClient;

namespace DAO
{
    public class MatHangXuatKhoDAO
    {
        #region 1.Retrieving
        public static List<MatHangXuatKhoDTO> SelectMatHangXuatKhoAllByID(string maMatHang, string maPhieuXuat)
        {
            List<MatHangXuatKhoDTO> listMatHang = new List<MatHangXuatKhoDTO>();

            List<SqlParameter> listSqlParam = new List<SqlParameter>();
            listSqlParam.Add(new SqlParameter("@MaMatHang", maMatHang));
            listSqlParam.Add(new SqlParameter("@MaPhieuXuat", maPhieuXuat));

            DataTable dt = SqlDataConnectHelper.ExecuteQuery("usp_SelectMatHangXuatKhosByAndMaMatHangMaPhieuXuat", listSqlParam);
            foreach (DataRow dr in dt.Rows)
            {
                MatHangXuatKhoDTO matHang = new MatHangXuatKhoDTO();
                matHang.MaMatHang = dr["MaMatHang"].ToString();
                matHang.MaPhieuXuat = dr["TenMatHang"].ToString();
                matHang.SoLuong = int.Parse(dr["SoLuong"].ToString());
                matHang.ThanhTien = float.Parse(dr["ThanhTien"].ToString());

                listMatHang.Add(matHang);
            }

            return listMatHang;
        }
        public static bool CheckIDMaMatHangExits(string maMatHang, string maPhieuXuat)
        {
            List<MatHangXuatKhoDTO> listMatHang = new List<MatHangXuatKhoDTO>();

            List<SqlParameter> listSqlParam = new List<SqlParameter>();
            listSqlParam.Add(new SqlParameter("@MaMatHang", maMatHang));
            listSqlParam.Add(new SqlParameter("@MaPhieuXuat", maPhieuXuat));

            DataTable dt = SqlDataConnectHelper.ExecuteQuery("usp_SelectMatHangXuatKhosByAndMaMatHangMaPhieuXuat", listSqlParam);
            foreach (DataRow dr in dt.Rows)
            {
                MatHangXuatKhoDTO matHang = new MatHangXuatKhoDTO();
                matHang.MaMatHang = dr["MaMatHang"].ToString();
                matHang.MaPhieuXuat = dr["MaPhieuXuat"].ToString();
                matHang.SoLuong = int.Parse(dr["SoLuong"].ToString());
                matHang.ThanhTien = float.Parse(dr["ThanhTien"].ToString());

                listMatHang.Add(matHang);
            }

            if (listMatHang.Count == 1)
            {
                return true;
            }
            else
                return false;
        }
        #endregion
        #region 2.Inserting
        public static bool InsertHangXuatKho(MatHangXuatKhoDTO hangXuatKho)
        {

            List<SqlParameter> listSql = new List<SqlParameter>();
            listSql.Add(new SqlParameter("@MaMatHang", hangXuatKho.MaMatHang));
            listSql.Add(new SqlParameter("@MaPhieuXuat", hangXuatKho.MaPhieuXuat));
            listSql.Add(new SqlParameter("@SoLuong", hangXuatKho.SoLuong));
            listSql.Add(new SqlParameter("@ThanhTien", hangXuatKho.ThanhTien));

            int n = SqlDataConnectHelper.ExecuteNoneQuery("usp_InsertMatHangXuatKho", listSql);
            if (n == -1)
            {
                return true;
            }
            else
                return false;

        }
        #endregion
    }
}
